import { Component, OnDestroy, OnInit } from '@angular/core';
import { AppRoutingModule } from '../../../app-routing.module';
import { Router } from '@angular/router';

@Component({
    selector: 'app-loading',
    templateUrl: './index.component.html',
    styleUrls: ['./index.component.scss']
})
export class LoadingComponent implements OnInit,OnDestroy {
    intervalId: any;
    loading = true;
    constructor(
        private appRoutingModule: AppRoutingModule,
        private route: Router
    ) {
    }

    ngOnInit() {
        this.loading = true;
        this.startLoop();
    }
    startLoop() {
        this.intervalId = setInterval(() => {
            console.log('loading waiting menu load')
            if (this.appRoutingModule.allRoutes.length != 0) {
                if (this.loading){
                    this.route.navigate(['403']).finally();
                }
                this.stopLoop();
            }
        }, 5000);
    }

    stopLoop() {
        if (this.intervalId) {
            clearInterval(this.intervalId);
            this.intervalId = null;
            console.log('loading end menu load')
        }
    }

    ngOnDestroy(): void {
        this.loading = false;
        this.stopLoop()
    }
}
